﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace GameServer
{

    class Vehicle
    {

        private string _VehicleCode;
        public string VehicleCode
        {
            get { return this._VehicleCode; }
        }
        private int _Health;
        public int Health
        {
            get { return this._Health; }
            set
            {
                this._Health = value;
                if (this._Health > this._MaxHealth)
                    this._Health = this._MaxHealth;
            }
        }
        private int _MaxHealth;
        public int MaxHealth
        {
            get { return this._MaxHealth; }
        }
        private int _SpawnInterval;
        public int SpawnInterval
        {
            get { return this._SpawnInterval; }
        }

        private string _ChangedCode;
        public string ChangedCode
        {
            get { return this._ChangedCode; }
            set { this._ChangedCode = value; }
        }

        private int _PositionX;
        public int PositionX
        {
            get { return this._PositionX; }
            set { this._PositionX = value; }
        }
        private int _PositionY;
        public int PositionY
        {
            get { return this._PositionY; }
            set { this._PositionY = value; }
        }
        private int _PositionZ;
        public int PositionZ
        {
            get { return this._PositionZ; }
            set { this._PositionZ = value; }
        }

        private int _AxisX;
        public int AxisX
        {
            get { return this._AxisX; }
            set { this._AxisX = value; }
        }
        private int _AxisY;
        public int AxisY
        {
            get { return this._AxisY; }
            set { this._AxisY = value; }
        }
        private int _AxisZ;
        public int AxisZ
        {
            get { return this._AxisZ; }
            set { this._AxisZ = value; }
        }

        public int OwnerTeam
        {
            get
            {
                for (int i = 0; i <= this._Seats.Length - 1; i++)
                {
                    if ((this._Seats[i]._Owner) != null)
                    {
                        GameServer.Virtual_Objects.Room.virtualRoom ownerRoom = this._Seats[i]._Owner.Room;
                        if (ownerRoom.getSide(this._Seats[i]._Owner) == 1)
                        {
                            return 1;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
                return -1;
            }
        }

        private VehicleSeat[] _Seats;
        public int GetSeat(GameServer.Virtual_Objects.User.virtualUser Client)
        {
            int playerIndex = -1;
            for (int i = 0; i <= _Seats.Length - 1; i++)
            {
                if (object.ReferenceEquals(_Seats[i]._Owner, Client))
                {
                    playerIndex = i; break; // TODO: might not be correct. Was : Exit For
                }
            }
            return playerIndex;
        }
        public bool TakeSeat(int index, GameServer.Virtual_Objects.User.virtualUser Client)
        {
            int clientTeam = Client.Room.getSide(Client);
            if ((clientTeam == 1 && this.OwnerTeam == 0) || (clientTeam == 0 && this.OwnerTeam == 1))
                return false;
            for (int i = 0; i <= this._Seats.Length - 1; i++)
            {
                if (object.ReferenceEquals(this._Seats[i]._Owner, Client))
                    this._Seats[i].LeaveSeat();
            }
            if (index >= 0 && index <= this._Seats.Length)
            {
                return this._Seats[index].TakeSeat(Client);
            }
            return false;
        }
        public bool Leave(GameServer.Virtual_Objects.User.virtualUser Client)
        {
            for (int i = 0; i <= this._Seats.Length - 1; i++)
            {
                if (object.ReferenceEquals(this._Seats[i]._Owner, Client))
                {
                    this._Seats[i].LeaveSeat();
                    return true;
                }
            }
            return false;
        }
        public bool Join(GameServer.Virtual_Objects.User.virtualUser Client)
        {
            for (int i = 0; i <= this._Seats.Length - 1; i++)
            {
                if (this.TakeSeat(i, Client))
                {
                    return true;
                }
            }
            return false;
        }
        public VehicleSeat FindSeat(GameServer.Virtual_Objects.User.virtualUser Client)
        {
            for (int i = 0; i <= this._Seats.Length - 1; i++)
            {
                if (object.ReferenceEquals(this._Seats[i]._Owner, Client))
                {
                    return this._Seats[i];
                }
            }
            return null;
        }

        public Vehicle(string _VehicleCode, int _MaxHealth, VehicleSeat[] _Seats, int _SpawnInterval, int[] _DefaultPosition, int[] _DefaultAxis)
        {
            this._VehicleCode = _VehicleCode;
            this._Health = _MaxHealth;
            this._MaxHealth = _MaxHealth;
            this._Seats = _Seats;
            foreach (VehicleSeat s in this._Seats)
            {
                s.TakeOwnership(this);
            }
            this._PositionX = _DefaultPosition[0];
            this._PositionY = _DefaultPosition[1];
            this._PositionZ = _DefaultPosition[2];
            this._AxisX = _DefaultAxis[0];
            this._AxisY = _DefaultAxis[1];
            this._AxisZ = _DefaultAxis[2];
        }
    }

}